package com.shell.customer.invitation.rx;

import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.customer.base.service.CusRecService;
import com.shell.customer.invitation.InvtEvent;
import com.shell.customer.po.CusInvt;
import com.shell.customer.po.CusRec;
import com.shell.feign.PubUserExtraFeign;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.governor.modules.message.keyword.SmsInviteMemberKeyword;
import com.shell.governor.modules.message.keyword.SmsInviteSupplierKeyword;
import com.shell.governor.modules.message.keyword.SmsInviteSupplierNotifyKeyword;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SendInvtMsgSubscriber extends OnErrorNotImplementedSubscriber<InvtEvent> {
	@Autowired
	private PubUserExtraFeign pubUserExtraFeign;
	@Autowired
	private CusRecService cusRecService;

	public void onApplicationEvent(InvtEvent event) {
		List<CusInvt> cusInvts = event.getCusInvts();
		for (CusInvt cusInvt : cusInvts) {
			if (cusInvt.getInviteeCusType().equals("MEMBER")) {
				String template = "MT02102";

				SmsInviteMemberKeyword keyWord = new SmsInviteMemberKeyword();
				keyWord.setCusName(CommonDataUtil.getCurEntName());
				keyWord.setUrl(SpringContextUtil.getProperty("customer.website"));
				MessageSender.sendMessageToPhone(cusInvt.getContactorPhone(), CommonDataUtil.getUserId(), template,
						keyWord);
			} else {
				String template = "MT02101";
				SmsInviteSupplierKeyword keyWord = new SmsInviteSupplierKeyword();
				keyWord.setCusName(CommonDataUtil.getCurEntName());
				keyWord.setInviteCode(
						this.pubUserExtraFeign.getInviteCode(CommonDataUtil.getUserId(), CommonDataUtil.getCurEntId()));
				keyWord.setUserName(CommonDataUtil.getUserName());
				keyWord.setUrl(SpringContextUtil.getProperty("customer.website"));
				MessageSender.sendMessageToPhone(cusInvt.getContactorPhone(), CommonDataUtil.getUserId(), template,
						keyWord);

				CusRec platform = this.cusRecService.getPlatform(null);
				template = "MT02111";
				SmsInviteSupplierNotifyKeyword smsInviteSupplierNotifyKeyword = new SmsInviteSupplierNotifyKeyword();
				smsInviteSupplierNotifyKeyword.setCusName(CommonDataUtil.getCurEntName());
				smsInviteSupplierNotifyKeyword.setInviteeName(cusInvt.getInviteeName());
				smsInviteSupplierNotifyKeyword.setContactorName(cusInvt.getContactorName());
				smsInviteSupplierNotifyKeyword.setContactorPhone(cusInvt.getContactorPhone());
				MessageSender.sendMessageByTmpl(CommonDataUtil.getUserId(), template, smsInviteSupplierNotifyKeyword,
						platform.getCusId());
			}
		}
	}
}
